﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace BusinessObjects.Logic.Base {
    public class DatabaseLogicHelper : LogicBase, IDisposable {
        private static volatile DatabaseLogicHelper _instance;
        private readonly static object _syncRoot = new Object();

        public DatabaseLogicHelper() { }
        public DatabaseLogicHelper(Datalayer.Databases Database) : base(Database) { }

        public static DatabaseLogicHelper Instance {
            get {
                if (_instance == null) {
                    lock (_syncRoot) {
                        if (_instance == null) {
                            _instance = new DatabaseLogicHelper();
                        }
                    }
                }

                return _instance;
            }
        }

        public static DatabaseLogicHelper SetInstance(Datalayer.Databases Database) {
            if (_instance == null) {
                lock (_syncRoot) {
                    if (_instance == null) {
                        _instance = new DatabaseLogicHelper(Database);
                    }
                }
            }

            return _instance;
        }

        #region Dispose
        public new void Dispose() {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        protected new virtual void Dispose(bool disposing) {
            if (disposing) {
                if (_instance != null) {
                    _instance.Dispose();
                    _instance = null;
                }
            }
        }

        ~DatabaseLogicHelper() {
            Dispose(false);
        }
        #endregion
    }
}